
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using GenTechDataModel;


namespace GenTechMyFamily
{
    public class MyFamily
    {


        public static GenTechModel Import(TextReader reader)
        {
            GenTechModel model = new GenTechModel();
            StringBuilder builder = null;
            int state = 0;

            // TODO - Doug - 11/29/2006 - This is some really ugly code; I just want to get something quick and dirty
            // working to press forward on the data model...all of this should be rewritten when the model is more solid.

            string line;
            while ((line = reader.ReadLine()) != null)
            {
                line = line.Trim();

                if ((line.Length == 0) || (line[0] == '#'))
                {
                    continue;
                }

                switch (state)
                {
                    case 0:
                        switch (line)
                        {
                            case "Census-1850-US":
                                state = 1;
                                break;

                            case "Census-1860-US":
                                state = 3;
                                break;

                            case "Census-1880-US":
                                state = 5;
                                break;

                            default:
                                // TODO - Doug - 11/29/2006 - Emit an error message, and skip the block
                                state = 99;
                                break;
                        }
                        break;

                    case 1:
                        if (line == "{")
                        {
                            state = 2;
                            builder = new StringBuilder();
                        }
                        else
                        {
                            // TODO - Doug - 11/29/2006 - Emit an error message, and skip the block
                            state = 99;
                        }
                        break;

                    case 2:
                        if (line == "}")
                        {
                            Console.WriteLine("1850:\n" + builder.ToString());
                            // TODO - parse the data sitting in builder
                            builder = null;
                            state = 0;
                        }
                        else
                        {
                            builder.AppendLine(line);
                        }
                        break;

                    case 3:
                        if (line == "{")
                        {
                            state = 4;
                            builder = new StringBuilder();
                        }
                        else
                        {
                            // TODO - Doug - 11/29/2006 - Emit an error message, and skip the block
                            state = 99;
                        }
                        break;

                    case 4:
                        if (line == "}")
                        {
                            Console.WriteLine("1860:\n" + builder.ToString());
                            // TODO - parse the data sitting in builder
                            builder = null;
                            state = 0;
                        }
                        else
                        {
                            builder.AppendLine(line);
                        }
                        break;

                    case 5:
                        if (line == "{")
                        {
                            state = 6;
                            builder = new StringBuilder();
                        }
                        else
                        {
                            // TODO - Doug - 11/29/2006 - Emit an error message, and skip the block
                            state = 99;
                        }
                        break;

                    case 6:
                        if (line == "}")
                        {
                            Parse1880Census(model, builder.ToString());
                            builder = null;
                            state = 0;
                        }
                        else
                        {
                            builder.AppendLine(line);
                        }
                        break;

                    case 99:    // error
                        if (line == "}")
                        {
                            state = 0;
                        }
                        break;
                }
            }

            return model;
        }



        private static void Parse1880Census(GenTechModel model, string data)
        {
            string[] lines = data.Split('\n');

            for (int i = 0; i < lines.Length - 1; i++)
            {
                Console.WriteLine("Line {0}: {1}", i, lines[i]);
                GenTechPersona p = new GenTechPersona();
                model.AddPersona(p);
            }
        }



        public static GenTechModel Import(Stream stream)
        {
            GenTechModel model = null;

            using (StreamReader reader = new StreamReader(stream))
            {
                model = Import(reader);
            }

            return model;
        }


        public static GenTechModel Import(FileInfo info)
        {
            GenTechModel model = null;

            using (Stream stream = info.OpenRead())
            {
                model = Import(stream);
            }

            return model;
        }


        public static GenTechModel Import(string fileName)
        {
            FileInfo info = new FileInfo(fileName);

            return Import(info);
        }

    }
}
